package com.niiwoo.civet.user.service.duboo;

import cn.jiguang.common.DeviceType;
import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.fastjson.JSON;
import com.niiwoo.civet.user.dto.request.JpushMessageRequestDTO;
import com.niiwoo.civet.user.dto.response.UserExtInfoResponseDTO;
import com.niiwoo.civet.user.service.MessageDubboService;
import com.niiwoo.civet.user.service.UserExtInfoDubboService;
import com.niiwoo.civet.user.service.local.LibraLoanLimitService;
import com.niiwoo.tripod.push.component.JpushClients;
import com.niiwoo.tripod.push.model.PushTarget;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * @Author: wuke
 * @Description:
 * @Date: Created in 17:40 2017/11/7
 */
@Slf4j
@Service(version = "1.0.0")
public class MessageDubboServiceImpl implements MessageDubboService {

    @Reference(version = "1.0.0")
    private UserExtInfoDubboService userExtInfoDubboService;
    @Autowired
    JpushClients jpushClients;

    @Autowired
    private LibraLoanLimitService libraLoanLimitService;

    public void jpushMessage(JpushMessageRequestDTO jpushMessageRequestDTO) {
        List<String> toUserIdList = jpushMessageRequestDTO.getToUserIdList();
        if(CollectionUtils.isEmpty(toUserIdList)){
            return;
        }
        List<PushTarget> pushTargetList = this.initPushTargetList(toUserIdList);
        log.info("极光推送,pushTargetList={}", JSON.toJSONString(pushTargetList));
        if(CollectionUtils.isEmpty(pushTargetList)){
            return;
        }
        jpushClients.sendNotification(jpushMessageRequestDTO.getTitle(),jpushMessageRequestDTO.getContent(),new HashMap<String,String>(),pushTargetList);
    }

    private List<PushTarget> initPushTargetList(List<String> toUserIdList){
        List<PushTarget> pushTargetList = new ArrayList<>();
        for(String userId:toUserIdList){
            UserExtInfoResponseDTO userExtInfoResponseDTO=userExtInfoDubboService.queryUserBasicInfo(userId);
            if(userExtInfoResponseDTO==null){
                log.error("极光推送,目标用户ID={},发生异常,原因是用户扩展信息不存在",userId);
                continue;
            }
            String jiuangPushId = userExtInfoResponseDTO.getJiuangPushId();
            if(!StringUtils.hasText(jiuangPushId)){
                log.error("极光推送,目标用户ID={},发生异常,原因是用户极光推送ID不存在",userId);
                continue;
            }
            DeviceType deviceType=transDeviceType(userExtInfoResponseDTO.getLastLoginDevice());
            if(deviceType==null){
                log.error("极光推送,目标用户ID={},发生异常,原因是用户最后设备类型不是APP(IOS或安卓)",userId);
                continue;
            }
            PushTarget pushTarget = new PushTarget(jiuangPushId,deviceType);
            pushTargetList.add(pushTarget);
        }
        return pushTargetList;
    }

    public DeviceType transDeviceType(Byte lastLoginDevice){
        if(lastLoginDevice == null){
            return null;
        }

        if(lastLoginDevice.intValue()==0){
            return DeviceType.Android;
        }else if(lastLoginDevice.intValue()==1){
            return DeviceType.IOS;
        }

        return null;
    }
}
